# -*- coding:utf-8 -*-
from time import time

from engine.handler.base_handler import BaseHandler
from enums.metadata import *


class LianjiaHandler(BaseHandler):
    """
    {'currentLevel': {'month': ['07月', '08月', '09月', '10月', '11月', '12月', '01月', '02月', '03月', '04月', '05月', '06月'],
    'listPrice': {'1_bed': [18927, 19539, 18698, 18786, 19271, 19471, 19513, 21515, 20821, 20180, 20076, 20084],
    '2_bed': [18671, 19114, 18878, 18800, 18727, 18960, 18361, 17486, 18690, 18501, 18478, 18519],
    '3_bed': [18228, 18832, 18810, 18541, 18461, 18744, 17904, 16675, 18301, 18362, 18207, 18122],
    'total': [18829, 19205, 19149, 18970, 18965, 19182, 18447, 17637, 18901, 18771, 18705, 18671],
    'other': [21364, 20633, 21548, 21361, 21427, 21399, 20181, 19789, 20920, 20288, 20491, 20466]},
    'dealPrice': {'1_bed': [16649, 16591, 16584, 16554, 16478, 16450, 16480, 16473, 16486, 16531, 16493, 16496],
    '2_bed': [16902, 16843, 16836, 16806, 16728, 16700, 16730, 16723, 16737, 16782, 16743, 16747],
    '3_bed': [16745, 16686, 16679, 16649, 16573, 16544, 16574, 16568, 16581, 16626, 16588, 16591],
    'total': [17119, 17059, 17053, 17022, 16944, 16915, 16945, 16938, 16952, 16998, 16959, 16962],
    'other': [19260, 19192, 19185, 19150, 19062, 19030, 19064, 19056, 19071, 19123, 19079, 19083]}}}
    """

    def handle(self, data):
        total_list = data["currentLevel"]["listPrice"]["total"]
        month_list = data["currentLevel"]["month"]

        result = []
        for i in range(len(total_list)):
            result.append({
                "firstType": FirstType.Estate.value,
                "secondType": EstateType.Lianjia.value,
                "source": "",
                "url": "",
                "province": "",
                "city": "",
                "staPeriod": StaPeriod.Month.name,
                "dateu": int(month_list[i][:2]),
                "price": total_list[i],
                "createTime": time(),
                "updateTime": time(),
            })
        return result

    @staticmethod
    def get_province_code():
        return "hubei"

    @staticmethod
    def get_city_code(lianjia_city):
        for name, value in LianjiaCity.__members__.items():
            if lianjia_city == name:
                return value.value.value
        raise Exception("unknown lianjia city")

@unique
class LianjiaCity(Enum):
    wh = City.Wuhan
